COP 3330-02 Object-Oriented Programming

 

Spring 2003 - Program Assignment 3

Due:  March 11, 2003

 

Objective:

Learn how to use Java arrays.  Learn how to use packages.

 

Description:

 

In this homework, you will essentially write a program that runs a simple bank.  Three classes will be used in this homework.  You have to implement these three classes.  The names of these classes are BankAccount, Bank, and BankApp.  They should be written and tested in this order.  These classes should contain the following instance methods. Make sure that all of your instance variables are declared as private.

 

BankAccount class:

 

The BankAccount class should be in the bank.support package.

 

A BankAccount object keeps information about one particular bank account at a bank.  The BankAccount class should contain following instance variables.  (The BankAccount class may contain static variables too).

 

Each bank account should have a unique account number.

private int accountNumber;

 

The owner of the account.

private String customerName;

 

The current balance of the account.

private double balance;

 

All the transactions (deposits and withdraws) on this account.  Withdraws are denoted with negative amounts and deposits with positive amounts.  All BankAccount objects can remember the past 20 transactions.  We do not worry about the situation where an account has over 20 transactions.

private double[] transactions;

 

The number of the transactions on the account.

private int transactionCount;

 

You have to include the following constructor and  instance methods in your BankAccount class:

 

Constructor:  Initializes a new bank account.  customerName and balance are initialized from the given values.  You have to generate a unique account number for the new BankAccount object.  The opening balance will be the first transaction (deposit) on this account.

public BankAccount(String customerName, double openingBalance)

 

Returns the account number of the bank account.

public int getAccountNumber()

 

Returns a String containing the account number, the customer name, and the balance of

the bank account.  Each value in the String should be labeled.

public String getAccountInfo()

 

Returns a String containing the last N transactions of the bank account.  The order in which transactions appear should be the order in which the transactions occurred.  If lastNTransactions > transactionCount, then print all of the transactions.

public String getTransactionInfo(int lastNTransactions)

 

Withdraws the given amount from the bank account.  It should also register this withdraw

amount as negative in the transaction history.  Note that the amount parameter of this method is not negative; we simply enter –1 * amount into the transaction history to denote it as a withdraw.  If the balance is not sufficient, it should print a message.

public void withdraw(double amount)

 

Deposits the given amount to the bank account.  It should also register this deposit as a transaction.

public void deposit(double amount)

 

Bank class:

 

The Bank class should be in the bank.main package.

 

A Bank object keeps information about the bank accounts at a bank.  The Bank class should contain following instance variables.  (The Bank class may contain static variables too).

 

All the bank accounts at this bank.

private BankAccount[] accounts;

 

The number of bank accounts at this bank.

private int accountCount;

 

You have to include the following constructor and instance methods in your Bank class:

 

Constructor:  Initializes the new Bank object.  This new Bank object initially doesn’t contain

any accounts but allows for up to accountCapacity accounts to be stored.  Note:  We do not worry about the situation where a Bank has over accountCapacity accounts.

public Bank(int accountCapacity)

 

Opens a new bank account and returns the account number of this new account.  Adds this account into the account list of the bank.  It takes the customer name and the opening balance as parameters.

public int openNewAccount(String customerName, double openingBalance)

 

Withdraws the given amount from the account whose account number is given.  If the account is not available at the bank, it should print a message.

public void withdrawFrom(int accountNum, double amount)

 

Deposits the given amount to the account whose account number is given.  If the account is not available at the bank, it should print a message.

public void depositTo(int accountNum, double amount)

 

Prints the account number, the customer name, and the balance of the bank account whose

account number is given.  If the account is not available at the bank, it should print a message.

public void printAccountInfo(int accountNum)

 

Prints the account number, the customer name, and the balance of the bank account whose

account number is given (together with last N transactions on that account).  If the account is not available at the bank, it should print a message.

public void printAccountInfo(int accountNum, int lastNTransactions)

 

BankApp class:

 

The BankApp class should be in the mystuff package.

 

You will create a console application that will "run" a bank.  Your BankApp class should contain only a main method. In this main method, you will create a Bank object first, and you will perform certain menu-driven operations using this Bank object. The main method of your BankApp class should do the following:

 

·        Create a new Bank object with an account capacity of 25.  You will use this Bank object for the operations.

 

·        Prompt the user with the following menu choices:

1) open a new bank account

2) deposit to a bank account

3) withdraw from a bank account

4) print short account info

5) print detailed account info including last transactions

6) quit

 

·        Execute the choice the user chooses and then loop until the user quits.

 

·        What each menu item choice should do:

1.      Ask the user for the customer name and the opening balance of a new bank account, and create this new bank account from that information at the bank.  Print the account number of this new account.

2.      Ask the user for the account number of an account and a deposit amount.  Deposit the given amount to the bank account whose account number is given.

3.      Ask the user for the account number of an account and a withdraw amount. Withdraw the given amount from the bank account whose account number is given.

4.      Ask the user for the account number of an account and print the short information (account number, customer name, and balance) about this account.

5.      Ask the user for the account number of an account and the number N, where N is the number of previous transactions to print.  Print the detailed information (account number, customer name, balance, and the last N transactions) about this account.

6.      Quit.

 

Submittal:

Submittals should follow the program specifications of the class.

 

Write-up:

There is no write-up required for this program.